package com.example.musicrecommend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.mapper.UserMapper;

import com.example.musicrecommend.model.entity.User;


import com.example.musicrecommend.service.UserService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {


    @Override
    public void validUser(User user, boolean isAdd) {
        ThrowUtils.throwIf(user == null, ErrorCode.PARAMS_ERROR);
        Long id = user.getId();
        String account = user.getAccount();
        String password = user.getPassword();
        String name = user.getName();
        String avatar = user.getAvatar();

        if (isAdd) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(account,password), ErrorCode.PARAMS_ERROR);
        } else {
            ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
            //?
            ThrowUtils.throwIf(account==null, ErrorCode.PARAMS_ERROR);
        }



        if (StringUtils.isNotBlank(name) && name.length() > 256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "昵称字数过长");
        }
        if(StringUtils.isNotBlank(avatar) && avatar.length()>256) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "头像图片url过长");
        }


    }

}
